package cn.kgc.servlet;

import cn.kgc.entity.Student;
import cn.kgc.service.StudentService;
import cn.kgc.service.impl.StudentServiceImpl;
import cn.kgc.util.Page;
import com.alibaba.fastjson.JSON;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.Timestamp;

@WebServlet(value = "/studentServlet")
public class StudentServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setContentType("text/html;");
        PrintWriter writer = response.getWriter();
        StudentService studentService = new StudentServiceImpl();
        String opr = request.getParameter("opr");
        switch (opr) {
            case "pageInfo":
                String pageNo = request.getParameter("pageNo");
                Page pageInfo = null;
                try {
                    pageInfo = studentService.getPageInfo(Integer.parseInt(pageNo));
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                String pageInfoStr = JSON.toJSONStringWithDateFormat(pageInfo, "yyyy-MM-dd hh:mm:ss");
                System.out.println("pageInfo:" + pageInfo.toString());
                writer.write(pageInfoStr);
                break;
            case "del":
                String stuNo = request.getParameter("studentNo");
                System.out.println("stuNo:" + stuNo);
                boolean flag = studentService.delStudent(stuNo);
                writer.write(JSON.toJSONString(flag));
                break;
            case "addStu":
                String studentNo = request.getParameter("studentNo");
                String studentName = request.getParameter("studentName");
                String sex = request.getParameter("sex");
                String bornDateStr = request.getParameter("bornDate").replace("T", " ");
                System.out.println("bornDateStr:" + bornDateStr);
                Timestamp bornDate = Timestamp.valueOf(bornDateStr);

                System.out.println("bornDate:" + bornDate);
                boolean insertFlag = studentService.insertStudent(new Student(Integer.parseInt(studentNo), studentName, sex, bornDate));
                if (insertFlag == true) {
                    writer.write("<script type=\"text/javascript\">");
                    writer.write(" alert(\"！新增成功！！success\");");
                    writer.write("window.location.href = \"http://localhost:8080/index.html\";");
                    writer.write("</script>");
                } else {
                    writer.write("<script type=\"text/javascript\">");
                    writer.write(" alert(\"新增失败！！！fail\");");
                    writer.write("window.location.reload()");
                    writer.write("</script>");
                }
                break;
            case "select":
                Student student = null;
                String studentNoSel = request.getParameter("studentNo");
                try {
                    student = studentService.select(studentNoSel);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                String studentStr = JSON.toJSONStringWithDateFormat(student, "yyyy-MM-dd hh:mm:ss").replace(" ", "T");
                System.out.println("studentStr++++++" + studentStr);
                writer.write(studentStr);
                break;
            case "updateStu":
                String studentNoUpd = request.getParameter("studentNo");
                String studentNameUpd = request.getParameter("studentName");
                String sexUpd = request.getParameter("sex");
                String bornDateUpd = request.getParameter("bornDate").replace("T", " ");
                System.out.println("bornDate" + bornDateUpd);
                Student studentUpd = new Student(Integer.parseInt(studentNoUpd), studentNameUpd, sexUpd, Timestamp.valueOf(bornDateUpd));
                boolean flagUpd = studentService.update(studentUpd);
                if (flagUpd == true) {
                    writer.write("<script type=\"text/javascript\">");
                    writer.write(" alert(\"！修改成功！！success\");");
                    writer.write("window.location.href = \"http://localhost:8080/index.html\";");
                    writer.write("</script>");
                } else {
                    writer.write("<script type=\"text/javascript\">");
                    writer.write(" alert(\"修改失败！！！fail\");");
                    writer.write("window.location.reload()");
                    writer.write("</script>");
                }
                break;

        }
        writer.flush();
        writer.close();

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doGet(request, response);
    }
}
